<?php
namespace App\Http\Controllers;

use App\Http\Requests\UserStatisticRequest;
use App\Http\Requests\ZtcStatisticRequest;
use App\Models\ThroughtrainApply;
use App\Models\User;
use Illuminate\Http\Request;
use \Excel;

class ZtcStatisticController extends CommonController
{
    /**
     * 获取用户统计页面
     */
    public function getIndex()
    {
        $total = User::count();

        return view('ztcstatistic.index')->with('total', $total);
    }

    /**
     *以日为单位获取对接次数
     */
    public function getDayUse()
    {
        $day = 24 * 3600;
        $today_zero = strtotime('today');
        //获取今天差几个小时
        $rest = sprintf("%.2f", (24 - (time() - $today_zero) / 3600));

        $date = array();
        //获取前31天的每天凌晨的时间戳,过去的时间排在前面
        $stamps = array();
        for ($i = 30; $i >= 0; $i--) {
            $stamps[] = $today_zero - $i * $day;
            $i > 0 && $date[] = date('m-d', strtotime(-$i . " day"));
        }

        //项目方申请并成功
        $pACounts = array();
        //投资方邀约并成功
        $iACounts = array();
        for ($i = 1; $i < 31; $i++) {
            //获取前三十天的注册数据，过去的数据放在前面
            $pACounts[] = ThroughtrainApply::where('status', 1)->where('is_send', 1)->whereBetween('begin_time', [$stamps[$i - 1], $stamps[$i]])->count();
            $iACounts[] = ThroughtrainApply::where('status', 1)->where('is_send', 0)->whereBetween('begin_time', [$stamps[$i - 1], $stamps[$i]])->count();
        }

        $pACounts[] = ThroughtrainApply::where('status', 1)->where('is_send', 1)->whereBetween('begin_time', [$today_zero, time()])->count();
        $iACounts[] = ThroughtrainApply::where('status', 1)->where('is_send', 0)->whereBetween('begin_time', [$today_zero, time()])->count();
        $date[] = '今天差' . $rest . '小时';

        $pAarr = $iAarr = [];
        foreach ($date as $k => $i) {
            $pAarr[] = array($i, $pACounts[$k]);
            $iAarr[] = array($i, $iACounts[$k]);
        }

        $arr = array('pAarr' => $pAarr, 'iAarr' => $iAarr);
        echo json_encode($arr);
    }

    /**
     * 以周为单位获取直通车对接次数
     */
    public function getWeekUse()
    {
        //获取本周还剩下几天
        $rest = (8 - date('w')) % 7;
        $rest == 0 && $rest = 7;

        //获取本周一凌晨时间戳
        $thisMonday = strtotime('today') - (7 - $rest) * 24 * 3600;

        // 定义一周的时间戳
        $week = 7 * 24 * 3600;

        //获取前30周的每周一凌晨的时间戳,从前往后排
        $stamps = array();
        $date = array();

        for ($i = 30; $i >= 0; $i--) {
            $stamps[] = $thisMonday - $i * $week;
            $i > 0 && $date[] = '前' . ($i) . '周';
        }

        //项目方申请并成功
        $pACounts = array();
        //投资方邀约并成功
        $iACounts = array();
        for ($i = 1; $i < 31; $i++) {
            $pACounts[] = ThroughtrainApply::where('status', 1)->where('is_send', 1)->whereBetween('begin_time', [$stamps[$i - 1], $stamps[$i]])->count();
            $iACounts[] = ThroughtrainApply::where('status', 1)->where('is_send', 0)->whereBetween('begin_time', [$stamps[$i - 1], $stamps[$i]])->count();
        }
        $pACounts[] = ThroughtrainApply::where('status', 1)->where('is_send', 1)->whereBetween('begin_time', [$thisMonday, time()])->count();
        $iACounts[] = ThroughtrainApply::where('status', 1)->where('is_send', 0)->whereBetween('begin_time', [$thisMonday, time()])->count();
        $date[] = '本周差' . $rest . '天';

        $pAarr = $iAarr = [];
        foreach ($date as $k => $i) {
            $pAarr[] = array($i, $pACounts[$k]);
            $iAarr[] = array($i, $iACounts[$k]);
        }

        $arr = array('pAarr' => $pAarr, 'iAarr' => $iAarr);
        echo json_encode($arr);
    }

    /**
     * 以月为单位获取直通车对接次数
     */
    public function getMonthUse()
    {
        //获取本月还剩下多少天
        $rest = date('t') - date('j') + 1;
        //获取本月1号凌晨的时间戳
        $thisMonth = strtotime(date('Y-m-01'));
        //获取过去十三个月的，每个月1号凌晨的时间戳，从前往后
        $m = date('m');
        $y = date('Y');
        $stamps = array();
        for ($i = 0; $i < 13; $i++) {
            $newM = $m - $i;
            if ($newM <= 0) {
                $newM += 12;
                $y = $y - 1;
            }
            $stamps[] = strtotime($y . '-' . $newM . '-01');
            $y = date('Y');
        }

        //项目方申请并成功
        $pACounts = array();
        //投资方邀约并成功
        $iACounts = array();
        for ($i = 12; $i > 0; $i--) {
            //获取前12个月的注册数据，过去的数据放在前面
            $pACounts[] = ThroughtrainApply::where('status', 1)->where('is_send', 1)->whereBetween('begin_time', [$stamps[$i], $stamps[$i - 1]])->count();
            $iACounts[] = ThroughtrainApply::where('status', 1)->where('is_send', 0)->whereBetween('begin_time', [$stamps[$i], $stamps[$i - 1]])->count();
        }

        $date = array();
        for ($i = 12; $i > 0; $i--) {
            //获取前12个月的文字表述，过去的表述放在前面
            $date[] = $m - $i < 1 ? '去年' . ($m - $i + 12) . '月' : $m - $i . '月';
        }

        $pACounts[] = ThroughtrainApply::where('status', 1)->where('is_send', 1)->whereBetween('begin_time', [$thisMonth, time()])->count();
        $iACounts[] = ThroughtrainApply::where('status', 1)->where('is_send', 0)->whereBetween('begin_time', [$thisMonth, time()])->count();
        $date[] = '本月差' . $rest . '天';

        $pAarr = $iAarr = [];
        foreach ($date as $k => $i) {
            $pAarr[] = array($i, $pACounts[$k]);
            $iAarr[] = array($i, $iACounts[$k]);
        }

        $arr = array('pAarr' => $pAarr, 'iAarr' => $iAarr);
        echo json_encode($arr);
    }

    /**
     * 导出数据
     */
    public function getExport(Request $request)
    {
        $data = $request->input();
        if (!$data['type'] || $data['type'] == '') {
            return $this->tip('操作错误，请选择类型', url('ztcstatistic/index'), false);
        }

        if (!$data['begin_time'] || $data['begin_time'] == '') {
            return $this->tip('操作错误，请选择开始时间', url('ztcstatistic/index'), false);
        }

        if (!$data['end_time'] || $data['end_time'] == '') {
            return $this->tip('操作错误，请选择结束时间', url('ztcstatistic/index'), false);
        }

        $data['end_time'] = strtotime($data['end_time']);
        $data['begin_time'] = strtotime($data['begin_time']);

        $uses = ThroughtrainApply::with('throughtrain.user', 'project', 'user')->whereBetween('begin_time', [$data['begin_time'], $data['end_time']])->get()->toArray();

//        dd($users);
        $title = [
            [
                "investName"  => '投资人用户名',
                "projectName" => '项目名',
                "realname"    => '项目方真实姓名',
                "phone"       => '项目方电话',
                "reason"      => '对接直通车理由',
                "begin_time"  => '开始时间',
                "end_time"    => '结束时间',
                "is_send"     => '类型',
                "status"      => '状态',
            ],
        ];

        $usesInfo = array();
        foreach ($uses as $k => $v) {
            $usesInfo[$k]['investName'] = isset($v['throughtrain']['user']['realname']) ? $v['throughtrain']['user']['realname'] : isset($v['throughtrain']['user']['nickname']) ? $v['throughtrain']['user']['nickname'] : $v['throughtrain']['user']['username'];
            $usesInfo[$k]['projectName'] = cut_str($v['project']['subject'], 8);
            $usesInfo[$k]['realname'] = isset($v['realname']) && $v['realname'] != '' ? $v['realname'] : isset($v['user']['realname']) ? $v['user']['realname'] : isset($v['user']['nickname']) ? $v['user']['nickname'] : $v['user']['username'];
            $usesInfo[$k]['phone'] = isset($v['phone']) && $v['phone'] != '' ? $v['phone'] : isset($v['user']) ? $v['user']['username'] : '';
            $usesInfo[$k]['reason'] = $v['reason'];
            $usesInfo[$k]['begin_time'] = date('Y-m-d H:i:s', $v['begin_time']);
            $usesInfo[$k]['end_time'] = date('Y-m-d H:i:s', $v['end_time']);
            $usesInfo[$k]['reason'] = $v['reason'];
            $usesInfo[$k]['is_send'] = $v['is_send'] == 1 ? '项目方申请' : $v['is_send'] == 0 ? '投资方邀约' : '';
            if (-1 == $v['status']) {
                $usesInfo[$k]['status'] = '拒绝';
            } elseif (0 == $v['status']) {
                $usesInfo[$k]['status'] = '等待中';
            } elseif (1 == $v['status']) {
                $usesInfo[$k]['status'] = '通过';
            } elseif (2 == $v['status']) {
                $usesInfo[$k]['status'] = '等待项目方应答';
            } elseif (-2 == $v['status']) {
                $usesInfo[$k]['status'] = '资方拒绝调整的时间';
            } else {
                $usesInfo[$k]['status'] = '';
            }
        }
        $usesInfo = array_merge($title, $usesInfo);
        Excel::create(
            '直通车对接列表',
            function ($excel) use ($usesInfo) {
                $excel->sheet(
                    '直通车对接列表',
                    function ($sheet) use ($usesInfo) {
                        $sheet->rows($usesInfo);
                    }
                );
            }
        )->export('xls');
    }

    public function postFind(ZtcStatisticRequest $request)
    {
        $data = $request->input();
        $data['end_time'] = strtotime($data['end_time']);
        $data['begin_time'] = strtotime($data['begin_time']);
        $uses = ThroughtrainApply::with('throughtrain.user', 'project', 'user')->whereBetween('begin_time', [$data['begin_time'], $data['end_time']])->paginate(10);
        $return_date = array();
        $uses_data = array();
//        dd($uses);
        foreach ($uses as $k => $v) {
            if (isset($v->throughtrain->user->realname)) {
                $uses_data[$k]['investName'] = $v->throughtrain->user->realname;
            } elseif (isset($v->throughtrain->user->nickname)) {
                $uses_data[$k]['investName'] = $v->throughtrain->user->nickname;
            } elseif (isset($v->throughtrain->user->username)) {
                $uses_data[$k]['investName'] = $v->throughtrain->user->username;
            } else {
                $uses_data[$k]['investName'] = '';
            }
            $uses_data[$k]['projectName'] = isset($v->project->subject) ? cut_str($v->project->subject, 8) : '';
            if (isset($v->realname) && $v->realname != '') {
                $uses_data[$k]['realname'] = $v->realname;
            } elseif (isset($v->user->realname)) {
                $uses_data[$k]['realname'] = $v->user->realname;
            } elseif (isset($v->user->nickname)) {
                $uses_data[$k]['nickname'] = $v->user->nickname;
            } elseif (isset($v->user->username)) {
                $uses_data[$k]['nickname'] = $v->user->username;
            } else {
                $uses_data[$k]['nickname'] = '';
            }
            if (isset($v->phone) && $v->phone != '') {
                $uses_data[$k]['phone'] = $v->phone;
            } elseif (isset($v->user)) {
                $uses_data[$k]['phone'] = $v->user->username;
            } else {
                $uses_data[$k]['phone'] = '';
            }
            $uses_data[$k]['reason'] = isset($v->reason) ? $v->reason : '';
            $uses_data[$k]['begin_time'] = date('Y-m-d H:i:s', $v->begin_time);
            $uses_data[$k]['end_time'] = date('Y-m-d H:i:s', $v->end_time);
            $uses_data[$k]['reason'] = $v->reason ? $v->reason : '';
            $uses_data[$k]['is_send'] = $v->is_send == 1 ? '项目方申请' : $v->is_send == 0 ? '投资方邀约' : '';
            if (-1 == $v->status) {
                $uses_data[$k]['status'] = '拒绝';
            } elseif (0 == $v->status) {
                $uses_data[$k]['status'] = '等待中';
            } elseif (1 == $v->status) {
                $uses_data[$k]['status'] = '通过';
            } elseif (2 == $v->status) {
                $uses_data[$k]['status'] = '等待项目方应答';
            } elseif (-2 == $v->status) {
                $uses_data[$k]['status'] = '资方拒绝调整的时间';
            } else {
                $uses_data[$k]['status'] = '';
            }
        }
//        dd($uses);
        $return_date['list'] = $uses_data;
        $return_date['page'] = $uses->render(true);

        return AjaxCallbackMessage($return_date, true);
    }
}